package com.chaoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.service.UploadClassifyVideoService;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * @Auther: 卜加超
 * @Date: 2018/12/27 16:07
 * @Description:
 */
@Controller
@RequestMapping("upload")
public class UploadClassifyVideoController {
    private Logger log = LoggerFactory.getLogger(UploadClassifyVideoController.class);

    @Resource
    private UploadClassifyVideoService uploadClassifyVideoService;

    /**
     * 新增视频上传
     * @param response
     * @param uploadFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadClassifyVideo")
    @ResponseBody
    public JSONObject uploadClassifyVideo(HttpServletResponse response, @RequestParam("file_data")Part uploadFile){
        String fileUrl = null;
        try{
            fileUrl = uploadClassifyVideoService.uploadClassifyVideo2WSCDN(uploadFile);
        }catch(Exception e){
            return UploadRes.error("文件上传失败,error:"+e.getMessage());
        }
        JSONObject result = UploadRes.success()
                .setFileUrl(fileUrl)
                .setFileType(FileType.VIDEO)
                .setDelelteUrl("deletefile")
                .putExtraParam("path", fileUrl).toJSON();
        return result;
    }

    /**
     * 新增视频封面上传
     * @param response
     * @param uploadFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadClassifyImg")
    @ResponseBody
    public JSONObject uploadClassifyImg(HttpServletResponse response, @RequestParam("file_data") Part uploadFile) throws IOException{
        String imgUrl = null;
        try{
            imgUrl = uploadClassifyVideoService.uploadClassifyImg2WSCDN(uploadFile);
        }catch(Exception e){
            return UploadRes.error("文件上传失败,error:"+e.getMessage());
        }
        JSONObject result = UploadRes.success()
                .setFileUrl(imgUrl)
                .setFileType(FileType.IMAGE)
                .setDelelteUrl("deletefile")
                .putExtraParam("path", imgUrl).toJSON();
        return result;
    }



}
